package com.app.qbake.module.publish.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hannesdorfmann.adapterdelegates3.AdapterDelegate;

import com.wind.base.adapter.DisplayItem;
import com.app.qbake.R;
import com.app.qbake.base.bean.UploadPhoto;
import com.app.qbake.module.publish.event.AddPhotoEvent;
import com.wind.view.WdImageLoader;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by wind on 2017/3/8.
 */

public class PhotoAddItemDelegate extends AdapterDelegate<List<DisplayItem>> {
    private LayoutInflater mLayoutInflate;
    private Activity mActivity;
    public static final String KEY_ADD_ITEM="key_add_item";
    public PhotoAddItemDelegate(Activity activity) {
        mLayoutInflate = LayoutInflater.from(activity);
        this.mActivity = activity;
    }
    private int mLayoutRes;
    public PhotoAddItemDelegate(Activity activity,int layoutRes) {
      this(activity);
      this.mLayoutRes=layoutRes;

    }

    @Override
    protected boolean isForViewType(@NonNull List<DisplayItem> items, int position) {
        UploadPhoto uploadPhoto = (UploadPhoto) items.get(position);
        if (uploadPhoto.getExtra()!=null){
            String val=uploadPhoto.getExtra().get(KEY_ADD_ITEM);
            if ("true".equals(val)){
                return true;
            }
        }

        return TextUtils.isEmpty(uploadPhoto.getPath()) && position!=0;
    }

    @NonNull
    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        int layout= R.layout.item_photo_add;
        if (mLayoutRes!=0){
            layout=mLayoutRes;
        }
        return new PhotoAddViewHolder(mLayoutInflate.inflate(layout, parent, false));
    }

    @Override
    protected void onBindViewHolder(@NonNull final List<DisplayItem> items, final int position, @NonNull RecyclerView.ViewHolder holder, @NonNull List<Object> payloads) {
        PhotoAddViewHolder vh = (PhotoAddViewHolder) holder;
        final UploadPhoto item = (UploadPhoto) items.get(position);
        WdImageLoader.display(mActivity, vh.iv, item.getResId());
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position==items.size()-1) {
                    EventBus.getDefault().post(new AddPhotoEvent());
                }


            }
        });
        if (vh.tv_name!=null) {
            String name = item.getExtra().get("name");
            vh.tv_name.setText(name);
        }
    }


    static class PhotoAddViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv)
        ImageView iv;

        @Nullable
        @BindView(R.id.tv_name)
        TextView tv_name;
        public PhotoAddViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

}
